package cn.zh.stan.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.Toast;

import com.hankkin.library.RefreshSwipeMenuListView;
import com.hankkin.library.SwipeMenu;
import com.hankkin.library.SwipeMenuCreator;
import com.hankkin.library.SwipeMenuItem;

import java.util.ArrayList;

import cn.zh.stan.ITem.CAItem;
import cn.zh.stan.MyApplication;
import cn.zh.stan.R;
import cn.zh.stan.adapter.CarAdapter;

public class CarActivity extends BaseActivity implements RefreshSwipeMenuListView.OnRefreshListener{

    TextView goOrder;
    RefreshSwipeMenuListView swipe;
    CarAdapter carAdapter;
    ArrayList<CAItem> caItems=new ArrayList<CAItem>();
    ArrayList<CAItem> datas=new ArrayList<CAItem>();
    MainActivity activty;
    String uid;
    CheckBox allchose;
    MyApplication mapp;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_car);
        mapp= (MyApplication)getApplication();
        initv();
       }



    public void initv(){
        goOrder= (TextView) findViewById(R.id.goOrder);
        swipe= (RefreshSwipeMenuListView) findViewById(R.id.swipe);
        allchose= (CheckBox) findViewById(R.id.allchose);
        uid=mapp.getSp().getString("uid","");
        SwipeMenuCreator creator = new SwipeMenuCreator() {
            @Override
            public void create(SwipeMenu menu) {
//                // 创建滑动选项
                // 创建删除选项
                SwipeMenuItem argeeItem = new SwipeMenuItem(getApplicationContext());
                argeeItem.setBackground(new ColorDrawable(getResources().getColor(R.color.del)));
                argeeItem.setWidth(dp2px(80, getApplicationContext()));
                argeeItem.setTitle("删除");
                argeeItem.setTitleSize(16);
                argeeItem.setTitleColor(Color.WHITE);
                menu.addMenuItem(argeeItem);
            }
        };
        swipe.setMenuCreator(creator);

        swipe.setOnMenuItemClickListener(new RefreshSwipeMenuListView.OnMenuItemClickListener() {
            @Override
            public void onMenuItemClick(int position, SwipeMenu menu, int index) {
                switch (index) {
                    case 0: //第一个选项
                        del(position,swipe.getChildAt(position+1-swipe.getFirstVisiblePosition()));
                        break;
                }
              }
        });

        swipe.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Intent intent=new Intent(CarActivity.this,DtailGoods.class);
                startActivity(intent);
            }
        });
        goOrder.setOnClickListener(this);

        carAdapter=new CarAdapter(this,caItems,allchose,mapp);
        swipe.setAdapter(carAdapter);
        swipe.setOnRefreshListener(this);
        allchose.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (mapp.isIschose()) {
                    if (b) {
                        for (int i = 0; i < caItems.size(); i++) {
                            caItems.get(i).setType(true);
                        }
                    } else {
                        for (int i = 0; i < caItems.size(); i++) {
                            caItems.get(i).setType(false);
                        }
                    }
                    carAdapter.notifyDataSetChanged();
                }else {
                    mapp.setIschose(true);
                }
            }
        });
      }



    @Override
    public void onRefresh() {
        swipe.postDelayed(new Runnable() {
            @Override
            public void run() {
                swipe.complete();
                Toast.makeText(CarActivity.this,"已完成",Toast.LENGTH_SHORT).show();

            }
        },2000);
    }

    @Override
    public void onLoadMore() {
        swipe.postDelayed(new Runnable() {
            @Override
            public void run() {
                swipe.complete();
                Toast.makeText(CarActivity.this,"已完成",Toast.LENGTH_SHORT).show();
            }
        },2000);
     }


    public  int dp2px(int dp, Context context) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp,
                context.getResources().getDisplayMetrics());
           }


    private void del(final int index, View v){
        final Animation animation = (Animation) AnimationUtils.loadAnimation(v.getContext(), R.anim.list_anim);
        animation.setAnimationListener(new Animation.AnimationListener() {
            public void onAnimationStart(Animation animation) {}
            public void onAnimationRepeat(Animation animation) {}
            public void onAnimationEnd(Animation animation) {
                caItems.remove(index);
                carAdapter.notifyDataSetChanged();
                animation.cancel();
            }
        });
        v.startAnimation(animation);
      }


}
